package com.hn658.base.message.notification.notify.dao.impl;

import com.hn658.base.message.notification.exception.NotificationException;
import com.hn658.base.message.notification.notify.dao.INotifyMongoDBDao;
import com.hn658.base.message.notification.notify.entity.NotificationEO;
import com.hn658.framework.dataaccess.mongo.AbstractMongoDBDaoImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by baihai on 2015/11/24.
 */
@Repository
public class NotifyMongoDBDaoImpl extends AbstractMongoDBDaoImpl<NotificationEO>
        implements INotifyMongoDBDao {


    @Override
    public Long getCount(Map<String, Object> queryParam) {
        if (queryParam == null) {
            return null;
        }
        Query query = queryParamInit(queryParam);
        return this.mongoTemplate.count(query, clazz);
    }


    @Override
    public List<NotificationEO> findByQueryMap(Map<String, Object> queryParam, int pageSize,
                                          int startIndex, String orderBy, boolean isAsc) {
        if (queryParam == null || pageSize < 0 || startIndex < 0) {
            return null;
        }
        Query query = queryParamInit(queryParam);
        logger.debug("======聊天消息Mongodb查询条件======："+query.toString());
        if(isAsc){
            query.with(new Sort(Sort.Direction.ASC, orderBy));
        }else{
            query.with(new Sort(Sort.Direction.DESC, orderBy));
        }
        query.skip(startIndex);
        if (pageSize > 0) {
            query.limit(pageSize);
        }
        return this.mongoTemplate.find(query, clazz);
    }

    @Override
    public NotificationEO findByNotifyId(String notifyId){
        if(StringUtils.isEmpty(notifyId)){
            return null;
        }
        Query query = new Query(Criteria.where("notifyId").is(notifyId));
        logger.debug("======聊天消息Mongodb查询条件======："+query.toString());
        return this.mongoTemplate.findOne(query,clazz);
    }


    @Override
    public void update(NotificationEO eo){

        if(eo==null||eo.getNotifyId()==null){

            throw new NotificationException(NotificationException.EmptyIdError);
        }

        Query query = new Query();

        query.addCriteria(Criteria.where("notifyId").is(eo.getNotifyId()));

        Update update = updateParamInit(eo);

        this.mongoTemplate.updateFirst(query,update,clazz);
    }

    private Query queryParamInit(Map<String, Object> param) {
        Map<String, Object> queryParam = new HashMap<String, Object>();
        queryParam.putAll(param);
        Query query = new Query();


        for (String key : queryParam.keySet()) {
            Object value = queryParam.get(key);
            if (value != null) {
                query.addCriteria(Criteria.where(key).is(value));
            }
        }
        return query;
    }


    private Update updateParamInit(NotificationEO eo){

        Update update = new Update();

        if(StringUtils.isNotEmpty(eo.getUuid())){

            update.set("uuid", eo.getUuid());
        }

//        if(StringUtils.isNotEmpty(eo.getNotifyId())){
//
//            update.set("notifyId",eo.getNotifyId());
//        }

        if(eo.getNotifyType()!=null){

            update.set("notifyType", eo.getNotifyType());
        }

        if(StringUtils.isNotEmpty(eo.getToUid())){

            update.set("toUid", eo.getToUid());
        }
        
        if(eo.getToGroupId()!=null){
        	
        	update.set("toGroupId", eo.getToGroupId());
        }

        if(StringUtils.isNotEmpty(eo.getToDeviceId())){

            update.set("toDeviceId", eo.getToDeviceId());
        }

        if(StringUtils.isNotEmpty(eo.getMsg())){

            update.set("msg", eo.getMsg());
        }

        if(eo.getFromUid()!=null){

            update.set("fromUid", eo.getFromUid());
        }
        
        if(StringUtils.isNotEmpty(eo.getFromDeviceId())){

            update.set("fromDeviceId", eo.getFromDeviceId());
        }

        if(eo.getNotifyDateTime()!= 0){

            update.set("notifyDateTime", eo.getNotifyDateTime());
        }

        if(eo.getIsDeleted()!=null){
            update.set("isDeleted", eo.getIsDeleted());
        }

        return update;

    }



    @Override
    public Long getMaxId() {
        Query query = new Query().with(new Sort(Sort.Direction.DESC, "_id"));
        NotificationEO messageEO = this.mongoTemplate.findOne(query, clazz);
        return messageEO == null ? 0 : messageEO.getId();
    }

}
